/*
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU Lesser General Public License Version
 *  3 as published by the Free Software Foundation.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU Lesser General Public License for more details.
 *
 *  You should have received a copy of the GNU Lesser General Public
 *  License along with this program; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307,
 *  USA.
 */
 
#include <optra/zoneinterface.h>

#include <QSettings>

#include <optra/zoneinfo.h>

#include <optra/common/proxy.h>
#include <optra/common/db.h>

using namespace Optra;

ZoneInterface::ZoneInterface(QObject *parent):
        QObject(parent)
{
    db_ = NULL;
}

ZoneInterface::~ZoneInterface()
{
    if (db_ != NULL){
        delete db_;
    }
}

DB* ZoneInterface::db()
{
    if (db_ == NULL){
        db_ = new DB(zoneInfo()->id(), this);
    }
    return db_;
}

void ZoneInterface::setProxy(Proxy* proxy)
{
    proxy_ = proxy;
}

Proxy* ZoneInterface::proxy()
{
    return proxy_;
}

Op::States ZoneInterface::state()const
{
    return state_;
}

ZoneInterface* ZoneInterface::optra()const
{
    Q_ASSERT(proxy_);
    return proxy_->mainZone();
}

void ZoneInterface::updateState(Op::States state)
{
    Q_ASSERT(zoneInfo());

    state_ = state;
    if (state_ == Op::Ready){
        emit zoneReady(zoneInfo()->name());
    }
}

bool ZoneInterface::initialize()
{
    return true;
}

void ZoneInterface::unload()
{
}

void ZoneInterface::launch()
{

}

QSettings& ZoneInterface::settings()
{
    return *settings_;
}

void ZoneInterface::setSettings(QSettings* settings)
{
    if (settings == NULL){
        qFatal("Settings is NULL)");
    }
    settings_ = settings;
}

QDir ZoneInterface::baseDir()const
{
    return base_;
}
void ZoneInterface::setBaseDir(const QDir& dir)
{
    base_ = dir;
}

bool ZoneInterface::install()
{
    return true;
}

void ZoneInterface::error(const QString& log)
{
    proxy()->error(log, this);
}

void ZoneInterface::critical(const QString& log)
{
    proxy()->critical(log, this);
}

void ZoneInterface::warning(const QString& log)
{
    proxy()->warning(log, this);
}

void ZoneInterface::info(const QString& log)
{
    proxy()->info(log, this);
}

void ZoneInterface::debug(const QString& log)
{
    proxy()->debug(log, this);
}

void ZoneInterface::trace(const QString& log)
{
    proxy()->trace(log, this);
}

